/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       actor.cpp
 * Author:     twoja stara
 * Created on: 2009-09-24
 *
 **********************************************************************************************************************/

#include "actor.h"

namespace mGameEngine
{
namespace Physics
{

Actor::Actor() :
    _joints(), _created(false), _object(NULL)
{
}

Actor::~Actor()
{
    // remove joints
    while(!_joints.empty())
    {
        delete _joints.front();
        // destructor and removeJoint() removes joint from list.
        // dont pop_front()!!
    }
}

void Actor::addJoint(Joint *joint, Actor *actor)
{
    // no NULL joints or self connections
    if(!joint || !actor || actor == this)
    {
        return;
    }

    // actor is already connected to joint
    for(List<Joint *>::ConstIterator i = _joints.begin(); i != _joints.end(); ++i)
    {
        if(*i == joint)
        {
            return;
        }
    }

    _joints.push_back(joint);

    // set joint actors
    joint->setFirstActor(this);
    joint->setSecondActor(actor);

    // create joint
    if(_created && actor->_created)
    {
        joint->create(_createJointActor(joint), actor->_createJointActor(joint));
    }
}

void Actor::removeJoint(Joint *joint)
{
    // no null joints
    if(!joint)
    {
        return;
    }

    _joints.remove(joint);

    // destroy joint if it's still connected to actors
    if(joint->getFirstActor() && joint->getSecondActor())
    {
        delete joint;
    }
}

void *Actor::getConnectedObject() const
{
    return _object;
}

void Actor::setConnectedObject(void *object)
{
    _object = object;
}

}
}

